package edu.ncu.service.impl;

import edu.ncu.dao.FriendDao;
import edu.ncu.dao.UserDao;
import edu.ncu.entity.Friend;
import edu.ncu.entity.User;
import edu.ncu.service.FriendService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("friendService")
public class FriendServiceImpl implements FriendService {
    private final FriendDao friendDao;
    private final UserDao userDao;

    public FriendServiceImpl(FriendDao friendDao, UserDao userDao) {
        this.friendDao = friendDao;
        this.userDao = userDao;
    }

    @Override
    public List<Friend> findAll() {
        System.out.println("进入了friendService");
        return friendDao.findAll();
    }

    /**
     * 找出userId用户的所有好友信息，返回好友列表
     * @param userId
     * @return
     */
    @Override
    public List<User> findByUserId(int userId) {
        List<Friend> friends = friendDao.findByUserId(userId);
        System.out.println("好友关系信息："+friends);
        List<User> theFriends = new ArrayList<>();
        for(Friend friend : friends)
        {
            //根据好友的id找出具体的信息
            User oneFriend = userDao.findByUserId(friend.getFriendId());
            theFriends.add(oneFriend);
        }
        return theFriends;
    }

    @Override
    public void saveFriend(Friend friend) {
        friendDao.saveFriend(friend);
    }

    @Override
    public Friend checkIsFriend(int userId, int friendId) {
        return friendDao.checkIsFriend(userId,friendId) ;
    }
}
